package com.ailiwu.www.ailiwu.Fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.base.BaseFragment;

import java.util.ArrayList;

/**
 * Author:    XiYoung
 * Version    V1.0
 * Date:      2015/12/6 000622:18
 * Description:
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * 2015/12/6 0006       XiYoung              1.0                   1.0
 * Why & What is modified:
 */
public class FragmentSearchMain extends BaseFragment{
    private TabLayout mTabClassify;
    private ViewPager mVpClassify;
    private Adapter mAdapter;
    protected ArrayList<Fragment> fragments = new ArrayList<>();
    protected ArrayList<String> tabList = new ArrayList<>();
    private final static String STR1 = "礼物";
    private final static String STR2 = "攻略";
    private final static String KEY = "key";
    private static String key;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arg = getArguments();
        if(arg != null){
            key = arg.getString(KEY, "");
        }
    }

    public static FragmentSearchMain newInstance(String arg) {
        Bundle args = new Bundle();
        args.putString(KEY,arg);
        FragmentSearchMain fragment = new FragmentSearchMain();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected View inflateView() {
        View view = View.inflate(getActivity(), R.layout.fragment_classify, null);
        fragments.clear();
        tabList.clear();
        fragments.add(FragmentSearchGift.newInstance(key));
        fragments.add(FragmentSearchCollection.newInstance(key));
        tabList.add(STR1);
        tabList.add(STR2);
        setTabViewPager(view);
        mTabClassify.setupWithViewPager(mVpClassify);
        mAdapter.notifyDataSetChanged();
        return view;
    }

    @Override
    protected void initView(View view) {
        /**设置ViewPager和Tab的适配*/


    }


    @Override
    protected void initData() {

    }

    private void setTabViewPager(View view) {
        mVpClassify = (ViewPager) view.findViewById(R.id.vp_classify);
        mTabClassify = (TabLayout) view.findViewById(R.id.tab_classify);
        mAdapter = new Adapter(getActivity().getSupportFragmentManager());
        mVpClassify.setAdapter(mAdapter);
    }
    /**
     * Tab与Viewpager适配器
     */
    class Adapter extends FragmentPagerAdapter {
        public Adapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabList.get(position);
        }
    }

}
